# coding=utf-8
import json
t = [
    (1, 'a', 'aa'),
    (1, 'b', 'bb'),
    (1, 'c', 'cc'),
    (1, 'd', 'dd'),
    (1, 'a', 'ee'),
    (1, 'b', 'ff'),
    (2, 'c', 'gg'),
    (2, 'd', 'hh'),
    (2, 'a', 'ii'),
    (2, 'b', 'jj'),
    (3, 'c', 'kk'),
    (3, 'd', 'll'),
]

d = {}
for i in t:
    if d.has_key('%s%s' % (i[0], i[1])):
        d['%s%s' % (i[0], i[1])].add(i[2])
    else:
        d['%s%s' % (i[0], i[1])] = set([i[2]])

d = {i: list(d[i]) for i in d}

print json.dumps(d, indent=4)
